//
//  FeedExtensions.m
//  Doc Bao
//
//  Created by Hai Nguyen on 9/14/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "FeedExtensions.h"
#import "AppDelegate_Shared.h"
#import "CoreDataHelper.h"

@implementation Feed (Extensions)

+(BOOL) initFeeds{
	NSError *err = nil;
	NSManagedObjectContext *managedObjectContext = [mainSharedAppDelegate managedObjectContext];
	if (managedObjectContext == nil)
	{
		return NO;
	}
	NSMutableArray *feedArray = [[[NSMutableArray alloc] init] autorelease];
	NSMutableDictionary *feedDict = [[NSMutableDictionary alloc] init];
	[feedDict setObject:@"1" forKey:@"Id"];
	[feedDict setObject:@"Trang Chu" forKey:@"Name"];
	[feedDict setObject:@"1" forKey:@"NewsId"];
	[feedDict setObject:@"http://vnexpress.net/rss/gl/trang-chu.rss" forKey:@"Url"];
	[feedArray addObject:feedDict];
	[feedDict release];
	
	feedDict = [[NSMutableDictionary alloc] init];
	[feedDict setObject:@"2" forKey:@"Id"];
	[feedDict setObject:@"Xa Hoi" forKey:@"Name"];
	[feedDict setObject:@"1" forKey:@"NewsId"];
	[feedDict setObject:@"http://vnexpress.net/rss/gl/xa-hoi.rss" forKey:@"Url"];
	[feedArray addObject:feedDict];
	[feedDict release];
	
	feedDict = [[NSMutableDictionary alloc] init];
	[feedDict setObject:@"3" forKey:@"Id"];
	[feedDict setObject:@"The Gioi" forKey:@"Name"];
	[feedDict setObject:@"1" forKey:@"NewsId"];
	[feedDict setObject:@"http://vnexpress.net/rss/gl/the-gioi.rss" forKey:@"Url"];
	[feedArray addObject:feedDict];
	[feedDict release];
	
	feedDict = [[NSMutableDictionary alloc] init];
	[feedDict setObject:@"4" forKey:@"Id"];
	[feedDict setObject:@"Kinh Doanh" forKey:@"Name"];
	[feedDict setObject:@"1" forKey:@"NewsId"];
	[feedDict setObject:@"http://vnexpress.net/rss/gl/kinh-doanh.rss" forKey:@"Url"];
	[feedArray addObject:feedDict];
	[feedDict release];
	
	feedDict = [[NSMutableDictionary alloc] init];
	[feedDict setObject:@"1" forKey:@"Id"];
	[feedDict setObject:@"Chinh Tri - Xa Hoi" forKey:@"Name"];
	[feedDict setObject:@"2" forKey:@"NewsId"];
	[feedDict setObject:@"http://tuoitre.vn/RssFeeds.aspx?ChannelID=3" forKey:@"Url"];
	[feedArray addObject:feedDict];
	[feedDict release];
	
	feedDict = [[NSMutableDictionary alloc] init];
	[feedDict setObject:@"2" forKey:@"Id"];
	[feedDict setObject:@"The Gioi" forKey:@"Name"];
	[feedDict setObject:@"2" forKey:@"NewsId"];
	[feedDict setObject:@"http://tuoitre.vn/RssFeeds.aspx?ChannelID=2" forKey:@"Url"];
	[feedArray addObject:feedDict];
	[feedDict release];
	
	feedDict = [[NSMutableDictionary alloc] init];
	[feedDict setObject:@"3" forKey:@"Id"];
	[feedDict setObject:@"The Thao" forKey:@"Name"];
	[feedDict setObject:@"2" forKey:@"NewsId"];
	[feedDict setObject:@"http://tuoitre.vn/RssFeeds.aspx?ChannelID=14" forKey:@"Url"];
	[feedArray addObject:feedDict];
	[feedDict release];
	
	feedDict = [[NSMutableDictionary alloc] init];
	[feedDict setObject:@"1" forKey:@"Id"];
	[feedDict setObject:@"The Thao" forKey:@"Name"];
	[feedDict setObject:@"6" forKey:@"NewsId"];
	[feedDict setObject:@"http://www.thanhnien.com.vn/Pages/default.aspx?Channel=Th%E1%BB%83+thao" forKey:@"Url"];
	[feedArray addObject:feedDict];
	[feedDict release];
	
	feedDict = [[NSMutableDictionary alloc] init];
	[feedDict setObject:@"1" forKey:@"Id"];
	[feedDict setObject:@"The Thao" forKey:@"Name"];
	[feedDict setObject:@"6" forKey:@"NewsId"];
	[feedDict setObject:@"http://www.thanhnien.com.vn/Pages/default.aspx?Channel=Th%E1%BB%83+thao" forKey:@"Url"];
	[feedArray addObject:feedDict];
	[feedDict release];
	
	for (int i = 0; i < [feedArray count]; i++) {
		NSDictionary *dict = [feedArray objectAtIndex:i];
		Feed *feed = [NSEntityDescription insertNewObjectForEntityForName:@"Feed" inManagedObjectContext:managedObjectContext];
		feed.Id = [NSNumber numberWithInt:[[dict objectForKey:@"Id"] intValue]];
		feed.Name = [dict objectForKey:@"Name"];
		feed.NewsId = [NSNumber numberWithInt:[[dict objectForKey:@"NewsId"] intValue]];
		feed.Url = [dict objectForKey:@"Url"];
	}
	[managedObjectContext save:&err];
	
	return YES;
}

+(Feed*) getFeed:(int) feedId{
	Feed *feed = nil;
	NSManagedObjectContext *managedObjectContext = [mainSharedAppDelegate managedObjectContext];
	if (managedObjectContext == nil) {
		return nil;
	}	
	NSMutableArray *feedArray;
	NSPredicate *predicate = [NSPredicate predicateWithFormat: @"FeedId = %d", feedId];	
	@try {
		feedArray = (NSMutableArray*) [CoreDataHelper searchObjectsInContext:@"Feed" :predicate :nil :NO :managedObjectContext];
		
		if ([feedArray count] != 0)
		{
			feed = [feedArray objectAtIndex:0];
		}
	} @catch (NSException * e) {
		NSLog(@"error: %@", [e description]);
	}
	
	return feed;
}

+(NSArray*) getFeeds:(int) newsId{
	NSManagedObjectContext *managedObjectContext = [mainSharedAppDelegate managedObjectContext];
	if (managedObjectContext == nil) {
		return nil;
	}	
	NSMutableArray *feedArray;
	NSPredicate *predicate = [NSPredicate predicateWithFormat: @"NewsId = %d", newsId];	
	@try {
		feedArray = (NSMutableArray*) [CoreDataHelper searchObjectsInContext:@"Feed" :predicate :nil :NO :managedObjectContext];

	} @catch (NSException * e) {
		NSLog(@"error: %@", [e description]);
	}
	
	return feedArray;
}

@end
